package com.kerrykidz.sms.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.authority.service.IAuUserService;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.sms.dao.model.SmsBatchCondEntity;
import com.kerrykidz.sms.dao.model.SmsBatchEntity;
import com.kerrykidz.sms.dao.model.SmsContEntity;
import com.kerrykidz.sms.dao.model.SmsOutBoxEntity;
import com.kerrykidz.sms.service.IBatchService;
import com.kerrykidz.system.service.IUserService;
import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.UserUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.io.JsonUtil;

@Controller
@RequestMapping("/sms/batch")
public class SmsBatchController {
	
	@Autowired
	private IBatchService batchService;
	@Autowired
	private IAuUserService auUserService;
	@Autowired
	private IUserService userService;
	
	/**
	 * 访问首页
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("home")
	public ModelAndView home() throws RDPException{
		ModelAndView model = new ModelAndView("sms/sms_list");
		
		/***
		 * 1、 需要获取分店列表
		 * 2、获取用户消息
		 */
		model.addObject("branAll", auUserService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		return model;
	}
	/**
	 * 条件查询数据
	 * @param session
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("seacheHome")
	@ResponseBody
	public SmsBatchEntity seacheHome(HttpSession session,SmsBatchEntity entity) throws RDPException{
		entity.setIspaging(true);
		List<SmsBatchEntity> batchList = batchService.findSmsBatchEntity(entity);
		entity.setResults(batchList);
		return entity;
	}
	/***
	 * 添加  - 跳转页面
	 * @param session
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月25日
	 */
	@RequestMapping("addBatchHome")
	@ResponseBody
	public ModelAndView addBatchHome() throws RDPException{
		ModelAndView model = new ModelAndView("sms/sms_add");
		model.addObject("branAll", auUserService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		model.addObject("userAll", JsonUtil.nonDefaultJsonUtil().toJson(userService.listUserByRoleId("106")));
		model.addObject("currentTime", DateUtil.formatDate("yyyy-MM-dd HH:mm:ss", new Date()));
		return model;
	}
	
	@RequestMapping("addBatch")
	@ResponseBody
	public SmsBatchEntity addBatch(SmsBatchEntity entity) throws RDPException{
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity.setPkBatchId(PrimaryKeyGenerator.getPrimaryKey(BusinessType.DX, UserUtil.getUserFromSession().getAbbrCode()));
		batchService.addSmsBatchEntity(entity);
		return entity;
	}
	
	@RequestMapping("updateBatchHome")
	@ResponseBody
	public ModelAndView updateBatchHome(SmsBatchEntity entity) throws RDPException{
		ModelAndView model = null;
		entity = batchService.findBySmsBatchId(entity.getPkBatchId());
		
		if(entity != null ){
			model = new ModelAndView("sms/sms_add");
			model.addObject("currentTime", DateUtil.formatDate("yyyy-MM-dd HH:mm:ss", new Date()));
			if(entity.getSrcType() != null && entity.getSrcType().equals("1")){
				SmsBatchCondEntity condEntity = new SmsBatchCondEntity();
				condEntity.setFkBatchId(entity.getPkBatchId());
				model.addObject("condList", JsonUtil.nonDefaultJsonUtil().toJson(batchService.findSmsBatchCondEntity(condEntity)));
				
			}else{
				model.addObject("condList","");
			}
			model.addObject("entity", entity);
			model.addObject("branAll", auUserService.findGblSecBranAll());
			model.addObject("branId", UserUtil.getUserFromSession().getBranId());
			model.addObject("userAll", JsonUtil.nonDefaultJsonUtil().toJson(userService.listUserByRoleId("106")));
			model.addObject("batchId", entity.getPkBatchId());
		}
		return model;
	}
	@RequestMapping("searcheInf")
	@ResponseBody
	public ModelAndView searcheInf(SmsBatchEntity entity) throws RDPException{
		ModelAndView model = null;
		entity = batchService.findBySmsBatchId(entity.getPkBatchId());
		
		if(entity != null ){
			model = new ModelAndView("sms/sms_add_info");
			model.addObject("currentTime", DateUtil.formatDate("yyyy-MM-dd HH:mm:ss", new Date()));
			if(entity.getSrcType() != null && entity.getSrcType().equals("1")){
				SmsBatchCondEntity condEntity = new SmsBatchCondEntity();
				condEntity.setFkBatchId(entity.getPkBatchId());
				model.addObject("condList", JsonUtil.nonDefaultJsonUtil().toJson(batchService.findSmsBatchCondEntity(condEntity)));
				
			}else{
				model.addObject("condList","");
			}
			model.addObject("entity", entity);
			model.addObject("branAll", auUserService.findGblSecBranAll());
			model.addObject("branId", UserUtil.getUserFromSession().getBranId());
			model.addObject("userAll", JsonUtil.nonDefaultJsonUtil().toJson(userService.listUserByRoleId("106")));
			model.addObject("batchId", entity.getPkBatchId());
		}
		return model;
	}
	
	@RequestMapping("updateBatch")
	@ResponseBody
	public SmsBatchEntity updateBatch(SmsBatchEntity entity) throws RDPException{
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		batchService.updateSmsBatchEntity(entity);
		return entity;
	}
	
	@RequestMapping("deleteBatch")
	@ResponseBody
	public void deleteBatch(SmsBatchEntity entity) throws RDPException{
		batchService.deleteSmsBatchEntity(entity);
	}
	
	@RequestMapping("updateBatchClose")
	@ResponseBody
	public void updateBatchClose(SmsBatchEntity entity) throws RDPException {
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		batchService.updateCloseSmsBatchEntity(entity);
	}
	
	/**
	 * 短信模板搜索
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("seacheTempl")
	@ResponseBody
	public SmsContEntity seacheTempl(SmsContEntity entity) throws RDPException{
		entity.setIspaging(true);
		entity.setResults(batchService.findSmsContEntity(entity));
		return entity;
	}
	/**
	 * 修改模板
	 * @param entity
	 * @throws RDPException 
	 * @date: 2014年9月25日
	 */
	@RequestMapping("templEdit")
	@ResponseBody
	public void templEdit(SmsContEntity entity) throws RDPException{
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		/*entity.setFkBranId(UserUtil.getUserFromSession().getBranId());*/
		batchService.updateSmsContEntity(entity);
	}
	/**
	 * 新增短信模板
	 * @param entity
	 * @param session
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("templAdd")
	@ResponseBody
	public void templAdd(SmsContEntity entity) throws RDPException{
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		/*entity.setFkBranId(UserUtil.getUserFromSession().getBranId());*/
		batchService.addSmsContEntity(entity);
	}
	
	/**
	 * 删除短信模板
	 * @param entity
	 * @param session
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("templDel")
	@ResponseBody
	public void templDel(SmsContEntity entity,HttpSession session) throws RDPException{
		batchService.deleteSmsContEntity(entity);
	}
	
	/**
	 * 搜索电话号码
	 * @param entity
	 * @param session
	 * @throws RDPException
	 * @date: 2014年9月28日
	 */
	@RequestMapping("searchSmsBox")
	@ResponseBody
	public SmsOutBoxEntity searchSmsBox(SmsOutBoxEntity entity) throws RDPException{
		entity.setIspaging(true);
		List<SmsOutBoxEntity> boxList = batchService.findSmsOutBoxEntity(entity);
		entity.setResults(boxList);
		return entity;
	}
	
	@RequestMapping("deleteSmsBox")
	@ResponseBody
	public void deleteSmsBox(SmsOutBoxEntity entity) throws RDPException{
		batchService.deleteSmsOutBox(entity.getFkBatchId(),entity.getPkSmsId());
	}
	
	/**
	 * 试试统计符合条件的个数
	 * @param entity
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月29日
	 */
	@RequestMapping("readTimeCount")
	@ResponseBody
	public int readTimeCount(SmsBatchCondEntity entity) throws RDPException{
		return batchService.findByBatchCondWherePhoneCount(entity);
	}
	
	/**
	 * 发送短信历史 记录
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping("searcheBatchHisHome")
	public ModelAndView searcheBatchHisHome() throws RDPException{
		ModelAndView model = new ModelAndView("sms/sms_list_his");
		model.addObject("branAll", auUserService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		return model;
	}
	/**
	 * 查询发送历史
	 * @param session
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping("searcheBatchHis")
	@ResponseBody
	public SmsBatchEntity searcheBatchHis(SmsBatchEntity entity) throws RDPException{
		List<SmsBatchEntity> batchList = batchService.findHisBatchWhere(entity);
		entity.setResults(batchList);
		return entity;
	}

}
